﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFWS.XHandler.Core;
using JKF.Substructure;
using JKF.Entities.Model;
using JKF.BusinessLayer.BLL;

using JKF.DataModule.SqlServerDAL;
using JKF.BusinessLayer;
using JKF.Entities;
using System.Data;
using JKF.DataModule;
namespace JKF.HandlerLayer.Sys
{
     public class SysParamValueHandler : BaseHandler
    {
        bllSYS_PARAM op_PARAM = new bllSYS_PARAM();
        bllSYS_PARAM_VALUE op_PARAM_VALUE = new bllSYS_PARAM_VALUE();
        bllSYS_ROLE sysRole = new bllSYS_ROLE();
        //SysParamValueSet op_SysParamValueSet = new SysParamValueSet();
   

        public override void Add(ResponseInf responseInf)
        {
            throw new NotImplementedException();
        }


        public override void Update(ResponseInf responseInf)
        {
            string roleName = sysRole.GetRoleNameByKeyBLL(bllSYS_USER.CurrentUser.ROLE_KEY.ToString());
            string op_child_type = Request.Params["op_child_type"];
            switch (op_child_type)
            {
                case "UpdataParam":
                    List<ParamaMode> Paramas = this.JsonSerializer.Deserialize<List<ParamaMode>>(this.F("Params"));
                    List<SYS_PARAM_VALUEinfo> pARAM_VALUEinfos = new List<SYS_PARAM_VALUEinfo>();
                    foreach (ParamaMode mode in Paramas)
                    {
                        SYS_PARAM_VALUEinfo param_value = new SYS_PARAM_VALUEinfo();
                        param_value.PARAM_KEY =mode.PARAM_KEY;
                        param_value.UNIT_KEY = bllSYS_USER.CurrentUser.SHOP_CODE.ToString();
                        
                        if (mode.VAL_TYPE == "2")
                        {
                            try
                            {
                                param_value.VAL = null;
                                param_value.VAL_DATE = Convert.ToDateTime(mode.VALUE);
                            }
                            catch (Exception ex)
                            {
                                throw new MyException("请输入正确的时间。消息：" + ex.Message);
                            }
                        }
                        else
                        {
                            param_value.VAL = mode.VALUE;
                            param_value.VAL_DATE = null;
                        }
                        if (roleName.IndexOf("总部")!=-1)
                        {//总局   
                            param_value.UNIT_KEY = null;
                        }
                        //else if (bllSYS_USER.CurrentUser.ROLE_KEY.ToString() == "03")
                        else
                        {//机构管理员
                            //param_value.UNIT_KEY = bllSYS_USER.CurrentUser.USER_KEY.ToString();
                            param_value.UNIT_KEY = bllSYS_USER.CurrentUser.SHOP_CODE;
                        }/*
                        else
                        {
                            throw new MyException("你所在机构，没有权限维护，该信息！请所在《'区域服务中心'或'机构管理员'维护》");
                        }*/
                        pARAM_VALUEinfos.Add(param_value);
                    }
                    op_PARAM_VALUE.Updata(pARAM_VALUEinfos);
                    CacheManage.Remove("SysParamValueFromCatch*");
                    break;
                default: throw new NotImplementedException();
            }
        }

        public override void Delete(ResponseInf responseInf)
        {
            throw new NotImplementedException();
        }

        public override void Select(ResponseInf responseInf)
        {
            string op_child_type = Request.Params["op_child_type"];
            string roleName = sysRole.GetRoleNameByKeyBLL(bllSYS_USER.CurrentUser.ROLE_KEY.ToString());

            switch (op_child_type)
            {
                case "GetParaList":
                    string select_type = string.Empty;
                    select_type = " NAME like '%" + Request.Params["SearchText"] + "%' ";

                    if (roleName.IndexOf("总部") != -1 || roleName.IndexOf("总部") != -1 || roleName.IndexOf("总监") != -1)
                    {//总店
                        select_type += " AND (PARAM_SCOPE = '1')";
                    }
                    else 
                    {//机构管理员
                        select_type += " AND PARAM_SCOPE = '2'";
                    }
                    List<SYS_PARAMinfo> pama_list = op_PARAM.GetListArray(select_type);
                    //按升序排序

                    pama_list.Sort(delegate(SYS_PARAMinfo p1, SYS_PARAMinfo p2) { return Comparer<decimal>.Default.Compare(decimal.Parse(p1.DISPLAY_ORDER.ToString()),decimal.Parse(p2.DISPLAY_ORDER.ToString())); });
                    responseInf.Data = pama_list;
                    break;
                case "GetParaValueByKey":
                    string paramKey = Request.Params["paramKey"];
                    responseInf.Message = op_PARAM_VALUE.GetParamValueByKey(paramKey);
                    break;
                case "GetParaValue":
                    string UNIT_KEY = string.Empty;
                    string DEPT_KEY = string.Empty;
                    if (roleName.IndexOf("总部") != -1)
                    {//总店
                        ParmaAll data = new ParmaAll();
                        data.Data = op_PARAM_VALUE.GetListArray("  UNIT_KEY is null");
                        data.paramaMode = new ParamaMode();
                        responseInf.Data = data;
                    }
                    else
                    {//普通店铺
                        UNIT_KEY = bllSYS_USER.CurrentUser.SHOP_CODE.ToString();
                        DEPT_KEY = "";

                        ParmaAll data = new ParmaAll();
                        data.Data = op_PARAM_VALUE.GetListArray(" UNIT_KEY = '" + UNIT_KEY + "'");
                        data.paramaMode = new ParamaMode();

                        responseInf.Data = data;
                    }


                    break;
                default: throw new NotImplementedException();
            }

            //throw new NotImplementedException();
        }

        public override void DoOther(ResponseInf responseInf)
        {
            throw new NotImplementedException();
        }


        class ParmaAll
        {
            public object Data;
            public ParamaMode paramaMode;
        }

        class ParamaMode
        {
            string _PARAM_KEY = string.Empty;

            public string PARAM_KEY
            {
                get { return _PARAM_KEY; }
                set { _PARAM_KEY = value; }
            }
            string _VAL_TYPE = string.Empty;

            public string VAL_TYPE
            {
                get { return _VAL_TYPE; }
                set { _VAL_TYPE = value; }
            }

            private string _VALUE;

            public string VALUE
            {
                get { return _VALUE; }
                set { _VALUE = value; }
            }
        }
    }
}
